package com.wicture.erp4android.ui;

import com.libs.zxing.CaptureActivity;
import com.wicture.erp4android.R;
import com.wicture.erp4android.fragment.ContentFragment;
import com.wicture.erp4android.fragment.ContentFragment.ViewSetter;
import com.wicture.erp4android.ui.view.RecyclePage;
import com.wicture.erp4android.ui.view.SortingPage;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainAct extends FragmentActivity implements OnClickListener{
	private static  String ContentTag = "recycle";
	private ContentFragment fragment;
	private RadioButton rb_sorting;
	private RadioButton rb_recycle;
	private RadioButton rb_setting;
	private ViewSetter setter;
	private View mFunctions;
	private FrameLayout fl_contentFragment;
private static final int FUNCTION_SORTING = 0;
private static final int FUNCTION_SENDING = 1;
private static final int FUNCTION_SETTING = 2;
private int mFunction = FUNCTION_SORTING;
private FrameLayout fl_content;
private TextView tv_title;
private SortingPage sortingPage;
private RecyclePage recyclePage;
public int step1 = 0;
public int step2 = 0;
private String orderString = null;
private SensorManager sensorManager;
private SensorEventListener sensorEventListener;
private int equit=0;//控制常量 用来控制返回键退出
private Sensor sensor;
/**
 * 初始化界面
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.act_main);
	//初始化底栏3个按钮
	rb_sorting = (RadioButton) findViewById(R.id.rb_sorting);
	rb_recycle = (RadioButton) findViewById(R.id.rb_recycle);
	rb_setting = (RadioButton) findViewById(R.id.rb_setting);
	fl_content = (FrameLayout) findViewById(R.id.fl_content);
	rb_sorting.setOnClickListener(this);
	rb_recycle.setOnClickListener(this);
	rb_setting.setOnClickListener(this);
	initFragment();


	sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
	sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
	sensorEventListener = new SensorEventListener() {
		
		@Override
		public void onSensorChanged(SensorEvent event) {
			//加速度3个矢量
			float xValues = Math.abs(event.values[0]);
			float yValues = Math.abs(event.values[1]);
			float zValues = Math.abs(event.values[2]);
			if(xValues>15||yValues>15||zValues>15){
				//响应为摇一摇事件
				if(TextUtils.isEmpty(sortingPage.tranceBoxCode)){
					if(step1%2==0){
						//打开扫描条形码 界面
						Intent intent1 = new Intent(MainAct.this,
								CaptureActivity.class);
						startActivityForResult(intent1, 0);
						step1++;
						Toast.makeText(MainAct.this, "摇一摇", Toast.LENGTH_SHORT).show();
						System.out.println("step1="+step1);
					}
				}
				//扫发货箱的摇一摇逻辑
				if(TextUtils.isEmpty(sortingPage.sendBoxCode)&&sortingPage.tranceBoxCode!=null){
					if(step2%2==0){
					Intent intent2 = new Intent(MainAct.this,CaptureActivity.class);
					startActivityForResult(intent2, 1);
					step2++;
					}
				}
				
			}
			
		}
		
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
			
		}
	};
	sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}

private void initFragment(){
	//获取碎片管理器
FragmentManager fm = getSupportFragmentManager();
//开启事务，准备用fragment替换帧布局
FragmentTransaction transaction = fm.beginTransaction();
//创建一个fragment
fragment = new ContentFragment(MainAct.this);
setter = new ViewSetter() {
	
	

	@Override
	public View setView() {
		
		sortingPage = new SortingPage(MainAct.this);
		mFunctions = sortingPage.mRootView;
		return mFunctions;
	}
};

//mFragmentView = View.inflate(MainAct.this, R.layout.fragment_content, null);

//System.out.println("分拣页面对象"+sortingPage.mRootView);


fragment.setView(setter);
transaction.replace(R.id.fl_content, fragment, ContentTag);
transaction.commit();//提交事务
System.out.println("调试"+fragment.mActivity);

}

@Override
public void onClick(View v) {
	switch (v.getId()) {
	case R.id.rb_sorting://
		if(mFunction!=FUNCTION_SORTING){
		mFunction = FUNCTION_SORTING;
		Toast.makeText(getApplicationContext(), "复核", Toast.LENGTH_SHORT).show();
	if(fragment!=null){
		fragment.setTitile("分拣专区");
	}
	sortingPage = new SortingPage(MainAct.this);
	fragment.changeView(sortingPage.mRootView);
		}
		break;
case R.id.rb_recycle:
	if(mFunction!=FUNCTION_SENDING){
	System.out.println("回收了");
	mFunction = FUNCTION_SENDING;
	
	if(fragment!=null){
		fragment.setTitile("回收专区");
	}
		recyclePage = new RecyclePage(MainAct.this);
	fragment.changeView(recyclePage.mRootView);
	sortingPage = null;
	}
		break;
case R.id.rb_setting:
	mFunction = FUNCTION_SETTING;
	break;

	default:
		break;
	}
	
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	// TODO Auto-generated method stub
	super.onActivityResult(requestCode, resultCode, data);
	
	
	if (resultCode == RESULT_OK) {
		Bundle bundle1 = data.getExtras();
		String scanResult = bundle1.getString("result");
		System.out.println(scanResult+"长度"+scanResult.length());
		if(requestCode==0){
		
			sortingPage.setString(requestCode, scanResult);
			
			sortingPage.checkOrder(scanResult);
				Toast.makeText(getApplicationContext(), "无此订单号", Toast.LENGTH_SHORT).show();
				step1++;
		
				System.out.println("新step1="+step1);
				System.out.println("中转箱"+sortingPage.tranceBoxCode);
		}else if (requestCode==1){
			
			
			//检查发货箱
			sortingPage.setString(requestCode, scanResult);
			sortingPage.checkSendBox();
			step2++;
		}
		else if (requestCode==2){
			recyclePage.setCode(scanResult);
			recyclePage.comitData();
		}
	}else if( resultCode == RESULT_CANCELED){
		step1 = 0;
		step2 = 0;
	}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

	if(keyCode==KeyEvent.KEYCODE_BACK){
		if(equit==0){
			equit++;
		Toast.makeText(MainAct.this, "再点一次退出", Toast.LENGTH_SHORT).show();
		new Thread(){

			@Override
			public void run() {
				try {
					Thread.sleep(3000);
					equit=0;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}.start();
		return true;
		}else if(equit==1){
			equit = 0;
			//注销传感器监听
			sensorManager.unregisterListener(sensorEventListener, sensor);
			return super.onKeyDown(keyCode, event);
		}
		
	}
	return super.onKeyDown(keyCode, event);
}


}
